#include "myformtable.h"

#include "qstandarditemmodel.h"
#include "qitemselectionmodel.h"

#include "mydialogsize.h"
#include "mydialoghead.h"
#include "mydialoglocate.h"



MyFormTable::MyFormTable(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MyFormTableClass())
{
    ui->setupUi(this);
    
    setCentralWidget(ui->tableView);

    model_item = new QStandardItemModel(4, 4, this);
    QStringList header;
    header << "姓名" << "性别" << "学位" << "部门";
    model_item->setHorizontalHeaderLabels(header);

    model_select = new QItemSelectionModel(model_item);

    ui->tableView->setModel(model_item);
    ui->tableView->setSelectionModel(model_select);

    connect(model_select, &QItemSelectionModel::currentChanged,
        this, &MyFormTable::do_model_changed);
}

MyFormTable::~MyFormTable()
{
    delete ui;
}

void MyFormTable::on_action_SetHeader_triggered()
{
    if (dialog_head == nullptr)
        dialog_head = new MyDialogHead(this);

    QStringList string_list;
    for (int i = 0; i < model_item->columnCount(); i++)
        string_list.append(model_item->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
    dialog_head->set_header_list(string_list);

    int ret = dialog_head->exec();
    if (ret == QDialog::Accepted)
    {
        model_item->setHorizontalHeaderLabels(dialog_head->get_header_list());
    }

}

void MyFormTable::on_action_Locate_triggered()
{
    MyDialogLocate* dialog_locate = new MyDialogLocate(this);
    dialog_locate->setAttribute(Qt::WA_DeleteOnClose);
    dialog_locate->setWindowFlag(Qt::WindowStaysOnTopHint);

    dialog_locate->set_spin_range(model_item->rowCount(), model_item->columnCount());
    QModelIndex current_index = model_select->currentIndex();
    if (current_index.isValid())
        dialog_locate->set_spin_value(current_index.row(), current_index.column());

    connect(dialog_locate, &MyDialogLocate::cell_text_changed, this, &MyFormTable::do_set_cell_text);

    //点击后，关闭产生对话框按钮的行为，……恢复
    connect(dialog_locate, &MyDialogLocate::change_action_enable, ui->action_Locate,&QAction::setEnabled);

    //对话框中的行列跟着选择改变
    connect(this, &MyFormTable::cell_index_changed, dialog_locate, &MyDialogLocate::set_spin_value);

    dialog_locate->show();
    dialog_locate->setModal(false);//非模态，可以编辑底下
}

void MyFormTable::on_tableView_clicked(const QModelIndex& index)
{
    emit cell_index_changed(index.row(), index.column());
}

void MyFormTable::do_model_changed(const QModelIndex& current, const QModelIndex& previous)
{
    if (current.isValid())
        ui->statusBar->showMessage(QString::asprintf("当前单元格：%d行， %d列", current.row(), current.column()) +
            "单元格内容" + model_item->itemFromIndex(current)->text());
}

void MyFormTable::do_set_cell_text(int row, int column, QString& text)
{
    QModelIndex index = model_item->index(row, column);
    model_select->clearSelection();
    model_select->setCurrentIndex(index, QItemSelectionModel::Select);
    model_item->setData(index, text, Qt::DisplayRole);
}

void MyFormTable::on_action_SetSize_triggered()
{
    MyDialogSize* dialog_table_size = new MyDialogSize();

    dialog_table_size->set_row_column(model_item->rowCount(), model_item->columnCount());
    dialog_table_size->setWindowFlag(Qt::MSWindowsFixedSizeDialogHint);//对话框固定大小

    int ret = dialog_table_size->exec();//显示
    if (ret == QDialog::Accepted)
    {
        model_item->setColumnCount(dialog_table_size->column_count());
        model_item->setRowCount(dialog_table_size->row_count());
    }

    delete dialog_table_size;
}


